package com.myBase.core.facade.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.myBase.core.base.entity.PageVo;
import com.myBase.core.dao.GoldcoinTradeRecordDao;
import com.myBase.core.entity.GoldCoinSettlementReq;
import com.myBase.core.entity.GoldcoinTradeRecord;
import com.myBase.core.facade.service.GoldcoinTradeRecordServiceFacade;
import com.myBase.common.persistence.Page;
import com.myBase.common.service.CrudAbstractService;

/**
 * 用户金币获取、消费、过期相关流水Service
 * @author WuJie
 * @version 2017-05-04
 */
@Service("goldcoinTradeRecordServiceFacade")
public class GoldcoinTradeRecordServiceFacadeImpl extends CrudAbstractService<GoldcoinTradeRecordDao, GoldcoinTradeRecord>
        implements GoldcoinTradeRecordServiceFacade {

    @Autowired
    private GoldcoinTradeRecordDao goldcoinTradeRecordDao;

    public GoldcoinTradeRecord get(String id) {
        return super.get(id);
    }

    public List<GoldcoinTradeRecord> findList(GoldcoinTradeRecord goldcoinTradeRecord) {
        return super.findList(goldcoinTradeRecord);
    }

    public Page<GoldcoinTradeRecord> findPage(Page<GoldcoinTradeRecord> page, GoldcoinTradeRecord goldcoinTradeRecord) {
        return super.findPage(page, goldcoinTradeRecord);
    }

    @Transactional(readOnly = false)
    public void save(GoldcoinTradeRecord goldcoinTradeRecord) {
        super.save(goldcoinTradeRecord);
    }

    @Transactional(readOnly = false)
    public void delete(GoldcoinTradeRecord goldcoinTradeRecord) {
        super.delete(goldcoinTradeRecord);
    }

    /**
     * * getGoldRecordList(查询用户金币流水 领取，使用，过期) 
     * @param   GoldcoinTradeRecord
     * @return   pageVo
     */
    @Transactional(readOnly = false)
    public PageVo<HashMap<String, Object>> getGoldRecordList(PageVo<HashMap<String, Object>> page,
            GoldcoinTradeRecord goldRecord) throws Exception {
        logger.info("查询金币流水 service 开始");
        int currentPage = page.getCurrentPage() - 1;
        int pageSize = page.getPageSize();
        int offset = currentPage * page.getPageSize();

        goldRecord.setOffset(offset);//封装起始索引
        goldRecord.setPageSize(pageSize);//封装每页显示条数

        // 查询用户金币流水总条数
        int goldCount = goldcoinTradeRecordDao.getGoldRecordCount(goldRecord);
        page.setRecordCount(goldCount);
        logger.info("查询金币流水总数" + goldCount);
        List<HashMap<String, Object>> golds = goldcoinTradeRecordDao.getGoldRecordList(goldRecord);
        page.setVoList(golds);
        logger.info("查询金币流水 service结束");
        return page;
    }

    @Override
    public HashMap<String, Object> getGoldRecordSum(int activityId) throws Exception {
        logger.info("营销 活动资金   查询发出,使用,过期金币 service 开始 ,activityId=" + activityId);
        HashMap<String, Object> goldRecordSum = goldcoinTradeRecordDao.getGoldRecordSum(activityId);
        logger.info("营销 活动资金   查询发出,使用,过期金币 service 结束");
        return goldRecordSum;
    }

    @Override
    public HashMap<String, Object> getGoldRecordSumByUserId(GoldcoinTradeRecord goldRecord) throws Exception {
        logger.info("pc 我的金币   查询 使用,过期金币 service 开始 ,userid=" + goldRecord.getUserid());
        HashMap<String, Object> goldSum = goldcoinTradeRecordDao.getGoldRecordSumByUserId(goldRecord);
        logger.info("pc 我的金币   查询 使用,过期金币 service 结束");
        return goldSum;
    }

    /**
    *findUserGoldGainList(用户金币领取查询) 
    */
    @Override
    public Page<GoldcoinTradeRecord> findUserGoldGainList(Page<GoldcoinTradeRecord> page,
            GoldcoinTradeRecord goldRecord) throws Exception {
        logger.info("用户金币领取查询 service 开始");
        goldRecord.setPage(page);
        page.setList(goldcoinTradeRecordDao.findUserGoldGainList(goldRecord));
        logger.info("用户金币领取查询  service 结束,总条数为:" + page.getCount());
        return page;
    }

    /**
     *findUserGoldOverdueList(用户金币过期查询) 
     */
    @Override
    public Page<GoldcoinTradeRecord> findUserGoldOverdueList(Page<GoldcoinTradeRecord> page,
            GoldcoinTradeRecord goldRecord) throws Exception {
        logger.info("用户金币过期查询 service 开始");
        goldRecord.setPage(page);
        page.setList(goldcoinTradeRecordDao.findUserGoldOverdueList(goldRecord));
        logger.info("用户金币过期查询  service 结束,总条数为:" + page.getCount());
        return page;
    }

    /**
     *getUserGoldCount(用户金币过期查询,查询总人数,金币总数量，根据设备分组数量) 
     */
    @Override
    public List<HashMap<String, Object>> getUserGoldCount(GoldcoinTradeRecord goldRecord) throws Exception {
        logger.info("用户金币过期查询,查询数量,不同设备数量 service 开始");
        List<HashMap<String, Object>> record = goldcoinTradeRecordDao.getUserGoldCountByEquipment(goldRecord);
        HashMap<String, Object> userGoldCount = goldcoinTradeRecordDao.getUserGoldCount(goldRecord);
        record.add(userGoldCount);
        logger.info("用户金币过期查询,查询数量,不同设备数量  service 结束");
        return record;
    }

    @Override
    public ArrayList<Map<String, Object>> getGoldCoinActivityCoinSumByDate(
            GoldCoinSettlementReq goldCoinSettlementReq) {
        ArrayList<String> list = goldCoinSettlementReq.getActivityIdList();
        if (list.size() == 0) {
            return new ArrayList<Map<String, Object>>();
        }
        StringBuilder activityIds = new StringBuilder();

        int i = 0;
        for (; i < list.size() - 1; i++) {
            activityIds.append(list.get(i));
            activityIds.append(",");
        }
        activityIds.append(list.get(i));

        String branchId = goldCoinSettlementReq.getBranchId();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        StringBuilder minDate = new StringBuilder(sdf.format(goldCoinSettlementReq.getDate()) + " 00:00:00");

        StringBuilder maxDate = new StringBuilder(sdf.format(goldCoinSettlementReq.getDate()) + " 23:59:59");

        ArrayList<Map<String, Object>> getGoldCoinActivityCoinSumByDate = (ArrayList<Map<String, Object>>) goldcoinTradeRecordDao
                .getGoldCoinActivityCoinSumByDate(activityIds.toString(), branchId, minDate.toString(),
                        maxDate.toString());
        return getGoldCoinActivityCoinSumByDate;
    }

    @Override
    public ArrayList<String> getGoldCoinActivityIdListByDate(String brancheId, Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        StringBuilder minDate = new StringBuilder(sdf.format(date) + " 00:00:00");//最小时间
        StringBuilder maxDate = new StringBuilder(sdf.format(date) + " 23:59:59");//最大时间
        ArrayList<Map<String, Object>> listMap = goldcoinTradeRecordDao.getGoldCoinActivityIdListByDate(brancheId,
                minDate.toString(), maxDate.toString());

        ArrayList<String> list = new ArrayList<String>();
        for (Map<String, Object> map : listMap) {
            if (map != null) {
                list.add(map.get("goldCoinActivity").toString());
            }
        }
        return list;
    }

	@Override
	public ArrayList<GoldcoinTradeRecord> getBeanListByDetailIdAccountIdSend(String accountId, String detailId) {
		return goldcoinTradeRecordDao.getBeanListByDetailIdAccountIdSend(accountId, detailId);
	}
    
    

}
